﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDBconnection;


namespace CamposInventory
{
    public partial class frmStock : Form
    {
        
        DatabaseConnection dbconn = new DatabaseConnection();
        int qtyValue, qtyAmount, Total;
        int i = 0;
        int Index;
        
        public DataTable tbl = new DataTable();
        public frmStock()
        {
            InitializeComponent();
           
            txtsearch.Click += new EventHandler(txtsearch_Click);
            
            btnOutstock.Click += new EventHandler(btnOutstock_Click);
            btnInstock.Click += new EventHandler(btnInstock_Click);
            btnSearch.Click += new EventHandler(btnSearch_Click);
            
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtsearch.Text == "")
            {
                MessageBox.Show("No Product Code has been Input", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                txtsearch.Focus();
                return;
            }
            dbconn.dbconnection();
            
            dbconn.loadData("SELECT * FROM tblstock WHERE Productcode LIKE '%" + txtsearch.Text + "%'");
            DataView dw = new DataView(dbconn.tbl);
            dw.RowFilter = string.Format("Productcode LIKE '%{0}'", txtsearch.Text);
            dataGridView1.DataSource = dw;
            if (dbconn.tbl.Rows.Count > 0)
            {
                tbl = dbconn.tbl;
                txtQty.Text = tbl.Rows[i][2].ToString();
                
            }
            else
            {
                MessageBox.Show("No Product has been Found", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

            dbconn.DBClose();
        }

        void btnOutstock_Click(object sender, EventArgs e)
        {
            if (txtNostocks.Text != "")
            {
                dbconn.dbconnection();
                qtyValue = int.Parse(tbl.Rows[i][2].ToString());
                qtyAmount = int.Parse(txtNostocks.Text);
                Total = qtyValue - qtyAmount;
                UpdateQty myQty = new UpdateQty();
                myQty.qty = Total.ToString();
                dbconn.UpdateQuantity(myQty, Index);
                MessageBox.Show("Stock removed");
                txtNostocks.Text = "";
                dbconn.DBClose();
            }
            else 
            {

                MessageBox.Show("Invalid Operation", "Try Again",
                   MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                Clearingvalue();
                return;
            
            
            
            }
        }

        void btnInstock_Click(object sender, EventArgs e)
        {
            if (txtNostocks.Text != "")
            {
                dbconn.dbconnection();
                qtyValue = int.Parse(tbl.Rows[i][2].ToString());
                qtyAmount = int.Parse(txtNostocks.Text);
                Total = qtyValue + qtyAmount;
                UpdateQty myQty = new UpdateQty();
                myQty.qty = Total.ToString();
                dbconn.UpdateQuantity(myQty, Index);
                MessageBox.Show("Stock added");


                dbconn.DBClose();
            }
            else
            {
                MessageBox.Show("Invalid Operation", "Try Again",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                Clearingvalue();
                return;
            }
        }

      
        void txtsearch_Click(object sender, EventArgs e)
        {
            txtsearch.Text = "";
        }

        void Clearingvalue()
        {
        
            txtQty.Clear();
        }
        
    }
}
